var type, isExport, tradeType, isGeneralTrade, isProcessTrade, isSpecialTrade;
$(function () {
//进出口
    type = $("input[name='declaration_type']").val();
//是否为出口，默认为出口
    isExport = type !== "I";
//贸易类型
    tradeType = $("input[name='tradeType']").val();
//是否为一般贸易
    isGeneralTrade = tradeType == "0";
//是否为加工贸易
    isProcessTrade = tradeType == "1";
//是否为特殊贸易
    isSpecialTrade = tradeType == "2";
});
var nextDeclarationConfig = {
    "contract_no": {action: "next", map: {export: "invoice_no", import: "company_co"}},
    "invoice_no": {action: "next", map: "company_co"},
    "company_co": {
        action: "next",
        map: {export: "buyer", import: "fahuo_company_co"}
    },
    "fahuo_company_co": {
        action: "next", map: {
            export: "trade_mode_code",
            import: "trade_mode_code"
        }
    },
    "trade_mode_code": {action: "next", map: "levymode_code"},
    "levymode_code": {action: "next", map: "arrived_country_code"},
    "transport_na": {action: "next", map: "voyage_no"},
    "buyer": {action: "next", map: "fahuo_company_co"},
    "arrived_country_code": {
        action: "next", map: {
            export: "trading_country_code",
            import: "trading_country_code"
        }
    },
    "trading_country_code": {
        action: "next",
        map: "arrived_port_code"
    },
    "arrived_port_code": {action: "next", map: "goods_count"},
    "goods_count": {action: "next", map: "goods_package_code"},
    "goods_package_code": {action: "next", map: "deal_mode_code"},
    "freight3_code": {
        action: "valueNext",
        rely: "input[name='deal_mode']",
        map: {
            export: {
                "C&F": "otherfee1_code",
                "CIF": "premium1_code",
                "市场价": "premium1_code",
                "垫仓": "gross_weight"
            }, import: {
                "FOB": "premium1_code",
                "市场价": "premium1_code",
                "垫仓": "premium1_code",
                "C&I": "otherfee1_code"
            }
        }
    },
    /*"gross_weight": {action: "next", map: "net_weight"},*/
    "gross_weight": {
        action: "valueNext",
        rely: "input[name='deal_mode']",
        map: {
            export: {
                "FOB": "net_weight",
                "C&F": "net_weight",
                "CIF": "premium1_code",
                "市场价": "premium1_code",
                "垫仓": "premium1_code",
                "C&I": "premium1_code"
            },
            import: {
                "FOB": "net_weight",
                "C&F": "net_weight",
                "CIF": "net_weight",
                "市场价": "net_weight",
                "垫仓": "net_weight",
                "C&I": "net_weight"
            }
        }
    },
    "net_weight": {action: "next", map: {export: "customs_type_code", import: "goods_addr_code"}},
    "premium3_code": {
        action: "valueNext",
        rely: "input[name='deal_mode']",
        map: {
            export: {
                "CIF": "otherfee1_code",
                "市场价": "otherfee1_code",
                "垫仓": "otherfee1_code",
                "C&I": "otherfee1_code"
            }, import: {
                "FOB": "otherfee1_code",
                "C&F": "otherfee1_code",
                "市场价": "otherfee1_code",
                "垫仓": "otherfee1_code"
            }
        }
    },
    "customs_type_code": {
        action: "valueNext",
        rely: "input[name='deal_mode']",
        map: {
            export: {
                "FOB": "goods_addr_code",
                "C&F": "goods_addr_code",
                "CIF": "goods_addr_code",
                "市场价": "goods_addr_code",
                "垫仓": "goods_addr_code",
                "C&I": "goods_addr_code"
            }, import: {
                "FOB": "premium1_code",
                "C&F": "goods_addr_code",
                "CIF": "gross_weight",
                "市场价": "premium1_code",
                "垫仓": "premium1_code",
                "C&I": "gross_weight"
            }
        }
    },
    "deal_mode_code": {
        action: "valueNext",
        rely: "input[name='deal_mode']",
        map: {
            export: {
                "FOB": "gross_weight",
                "C&F": "freight1_code",
                "CIF": "freight1_code",
                "市场价": "freight1_code",
                "垫仓": "freight1_code",
                "C&I": "gross_weight"
            }, import: {
                "FOB": "freight1_code",
                "C&F": "premium1_code",
                "CIF": "customs_type_code",
                "市场价": "freight1_code",
                "垫仓": "freight1_code",
                "C&I": "freight1_code"
            }
        }
    },
    "otherfee3_code": {
        action: "valueNext",
        rely: "input[name='deal_mode']",
        map: {
            export: {
                "FOB": "goods_addr_code",
                "C&F": "goods_addr_code",
                "CIF": "goods_addr_code",
                "市场价": "other_goods_addr",
                "垫仓": "other_goods_addr",
                "C&I": "other_goods_addr"
            }, import: {
                "FOB": "customs_type_code",
                "C&F": "customs_type_code",
                "CIF": "customs_type_code",
                "市场价": "customs_type_code",
                "垫仓": "customs_type_code",
                "C&I": "customs_type_code"
            }
        }
    },
    "goods_addr_code": {action: "next", map: "remark"},
    "other_goods_addr": {action: "next", map: "customs_type_code"},
    "#name": {action: "next", map: "#count"},
    "#goods_mode": {action: "next", map: {export: "#count", import: "#levyModeCode"}},
    "#key_elements": {action: "next", map: "#count"},
    "#total_price": {action: "next", map: "#legal_count"},
    "#currencyCode": {
        action: "next",
        map: {export: "#destination_code", import: "#origin_country_code"}
    },
    "#origin_country_code": {
        action: "next",
        map: {import: "#destination_code", export: "#levyModeCode"}
    },
    "#legal_count": {
        action: "funCall",
        func: function ($input) {
            if ($("#second_count").parent("div").attr("class").indexOf("disabled") != -1) {
                $input.nextDeclarationInput({action: "next", map: "#currencyCode"});
            } else {
                $input.nextDeclarationInput({action: "next", map: "#second_count"});
            }
        }
    },
    "#second_count": {
        action: "next", map: {
            export: "#currencyCode",
            import: "#currencyCode"
        }
    },
    "#goods_net_weight": {
        action: "funCall",
        func: function ($input) {
            if (isExport) {
                $input.blur();
                $("#goodsSave").click();
            }
        }
    },
    "#destination_code": {
        action: "funCall",
        func: function ($input) {
            if (!isExport) {
                $("#goodsSave").click();
            } else {
                $input.nextDeclarationInput({action: "next"});
            }
        }
    },
    "#containerWeight": {
        action: "funCall",
        func: function ($input) {
            if (isExport) {
                $("#containersSave").click();
            }
        }
    },
    "#suifudan_no": {
        action: "funCall",
        func: function ($input) {
            if (isExport) {
                $("#attachedSave").click();
            }
        }
    },
    "remark": {
        action: "funCall",
        func: function ($input) {
            var commoForm = $("div#commodities:hidden");
            if (commoForm.length == 1) {
                $("#goodsAdd").click();
                /*$("div#commodities:hidden").show();*/
            }
            $input.nextDeclarationInput({action: "next"});
        }
    },
    "#name": {
        action: "funCall",
        func: function ($input) {
            var name = $input.val();
            if (name) {
                layer.open({
                               type: 2,
                               title: '商品信息',
                               area: ["600px", "300px"],
                               offset: ['12%', (window.document.body.clientWidth - 600) / 2],
                               content: $.url(
                                   "/business/goodsView-main?type=3.0&goodsViewName=" + name),
                               success: function (layero, index) {
                                   var f = layero.find("iframe")[0];
                                   $(f).height(layero.height() * 0.8);
                               }
                           });
                $input.nextDeclarationInput({action: "next"});
            }
        }
    },
    "itemNo": {
        action: "funCall",
        func: function ($input) {
            $(".relation-sure").click();
            if ($(".item-Number").css("border-color") == "rgb(255, 0, 0)"
                && ($(".item-Number").val() != undefined)
                && ($(".item-Number").val() != '')) {
                $(".item-Number").css("border-color", "#ddd");
            }
            if (relationBool && !$(".relation-sure").hasClass("disabled")) {
                $input.nextDeclarationInput({action: "next"});
            }
        }
    },
    methods: {
        next: function (customSetting) {
            var $input = $(this);
            var name = $input.attr("name");
            if (name == undefined) {
                name = $input.attr("id");
                name = "#" + name;
            }
            var nextSetting = nextDeclarationConfig[name];
            if (customSetting != undefined) {//加入传入了自定义的配置，则使用传入的参数
                nextSetting = customSetting;
            }
            if ("object" == typeof nextSetting) { //判断跳转的类型
                var action = nextSetting.action;
                var map = nextSetting.map;
                if ("next" == action) {
                    if (map != undefined) {
                        nextDeclarationConfig.methods.normalMapNext(nextSetting);
                    } else {
                        nextDeclarationConfig.methods.normalNext($input);
                    }
                }
                if ("valueNext" == action) {
                    nextDeclarationConfig.methods.valueNext(nextSetting);
                }
                if ("funCall" == action) {
                    nextSetting.func($input);
                }
            } else {
                var _$input = $input.parent("div").find("input:text");
                if (_$input.length == 0) {
                    _$input = $input.parent().find("input:text");
                }
                nextDeclarationConfig.methods.indexNext(_$input);
            }
        },
        normalNext: function ($input) {
            var _$input = $input.parent("div").find("input:text");
            if (_$input.length == 0) {
                _$input = $input.parent().find("input:text");
            }
            this.indexNext(_$input);
        },
        normalMapNext: function (setting) {
            var map = setting.map;
            var _name;
            if (typeof map == "string") {
                _name = map;
            } else {
                _name = isExport ? map.export : map.import;
            }
            var $target;
            if (/^#/.test(_name)) { //适应商品、柜、随附单的input
                $target = $("input" + _name);
            } else {
                $target = $("input[name='" + _name + "']");
            }
            if (this.isInvalidElement($target)) {
                $target.nextDeclarationInput();
            } else {
                var targetIsDropdown = $target.parent("div.dropdown").length == 1;
                if (targetIsDropdown) {
                    $target = $($target.parent("div.dropdown").find("input:text")[0]);
                }
                $target.focus();
            }

        },
        indexNext: function ($input) {
            if ($input.length == 0) {
                return;
            }
            var currentIndex = $input.attr("jumpIndex");
            currentIndex = parseInt(currentIndex);
            if (isNaN(currentIndex) || currentIndex == $.data.inputsLength) {
                return;
            }
            var $input__ = $("input[jumpIndex='" + (currentIndex + 1) + "']");
            if (this.isInvalidElement($input__)) {
                this.indexNext($input__);
            } else {
                $input__.focus();
                window.event.stopPropagation();
            }

        },
        valueNext: function (setting) {
            var relySelector = setting.rely;
            var $rely = $(relySelector);
            var val = $rely.val();
            var map = setting.map;
            var _name = isExport ? map.export[val] : map.import[val];
            var $target = $("input[name='" + _name + "']");
            var targetIsDropdown = $target.parent("div.dropdown").length == 1;
            if (targetIsDropdown) {
                $target = $($target.parent("div.dropdown").find("input:text")[0]);
            }
            $target.focus();
        },
        isInvalidElement: function ($input) {
            return $input.parent("div.disabled").length == 1
                   || $input.parent(".disabled").length == 1 || $input.parent(':visible') == 0;
        }
    }
};

$.fn.nextDeclarationInput = nextDeclarationConfig.methods.next;
